<?php

declare(strict_types=1);

namespace app\api\controller\v2;

use think\facade\Db;
use think\facade\Request;

class Info
{
    /**
     * 显示订单详情
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        $order = Db::name('pc_mall_order_form')->where('id', $id)->find();
        if ($order) {
            $code = 200;
            $msg = '获取成功';
            $data = $order;
        } else {
            $code = 404;
            $msg = '订单不存在';
            $data = null;
        }
        return result($data, $msg, $code);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->param();
        $result = Db::table('pc_mall_order_form')->save($data);

        if ($result == 1) {
            $data = [
                $data,
            ];
            $code = 201;
            $msg = '创建成功';
            return result($data, $msg, $code);
        }
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $data = Db::table('pc_mall_order_form')->select();
        $data = [
            $data,
        ];
        $code = 200;
        $msg = '获取成功';
        return result($data, $msg, $code);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->param();
        $result = Db::table('pc_mall_order_form')->where('id', $id)->update($data);

        if ($result == 1) {
            $data = [
                $data,
            ];
            $code = 200;
            $msg = '修改成功';
            return result($data, $msg, $code);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $data = Db::table('pc_mall_order_form')->where('id', $id)->find();
        $result2 = Db::table('pc_mall_order_form')->where('id', $id)->delete();

        if ($result2 == 1) {
            $data = [
                $data,
            ];
            $code = 202;
            $msg = '删除成功';
            return result($data, $msg, $code);
        }
    }

    /**
     * 根据订单号和交易号搜索符合条件的订单记录
     *
     * @param Request $request - 请求对象，包含查询订单号和查询交易号
     * @return \think\Response
     */
    public function search(Request $request)
    {
        $orderNo = $request->param('orderNo', '');
        $transactionNo = $request->param('transactionNo', '');

        if ($orderNo === '' && $transactionNo === '') {
            return json([
                'code' => 400,
                'msg' => '请至少提供一个查询条件',
                'data' => null
            ]);
        }

        $query = Db::name('pc_mall_order_form');
        if ($orderNo !== '') {
            $query->where('order_no', 'like', "%$orderNo%");
        }

        if ($transactionNo !== '') {
            $query->where('transaction_no', 'like', "%$transactionNo%");
        }

        $orders = $query->select();

        return json([
            'code' => 200,
            'msg' => '查询成功',
            'data' => [
                'orders' => $orders,
            ]
        ]);
    }
}
